/*
 * @Descripttion:
 * @Author: YuZhenJing
 * @Date: 2020-01-19 16:38:32
 * @LastEditors  : YuZhenJing
 * @LastEditTime : 2020-01-20 09:39:03
 */

/**
 * 获取可分配的菜单信息
 */
export function getMenuData() {
  const treeData = this.$html.get('roleFun/menuTreeData')
  treeData
    .then(data => {
      if (data.returnType == 'success') {
        this.menuTreeData = data.returnData
        this.getRoleFunListCodes()
      }
    })
    .catch(() => {
      console.log('error:roleJoinMenu/getMenuData')
    })
}

/**
 * 根据角色ID获取功能清单编码
 */
export function getRoleFunListCodes() {
  const resultData = this.$html.get('roleFun/roleFunListCodes/' + this.roleId)
  resultData
    .then(data => {
      if (data.returnType == 'success') {
        this.defaultCheckedKeys = data.returnData
      }
    })
    .catch(() => {
      console.log('error:roleJoinMenu/getRoleFunListCodes')
    })
}

/**
 * 保存选择的功能清单
 */
export function submitJoinFun() {
  this.saveLoading = true
  let funListPointsArray = []
  for (let key in this.funListPoints) {
    funListPointsArray.push({
      funListCode: key,
      pointCodes: this.funListPoints[key]
    })
  }
  let params = {
    roleId: this.roleId,
    funListCodes: this.getCheckedData(),
    funListPoints: funListPointsArray
  }
  const saveJoin = this.$html.postJson('roleFun/saveRoleMenu', params)
  saveJoin
    .then(data => {
      if (data.returnType == 'success') {
        this.saveLoading = false
        this.$notify({
          title: '成功',
          message: '角色与功能关联成功',
          type: 'success'
        })
        this.tfVisible = false
      }
    })
    .catch(() => {
      this.saveLoading = false
      console.log('error:roleJoinMenu/submitJoinFun')
    })
}
